-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.c
More file actions
38 lines (31 loc) · 822 Bytes
/
Solution.c
File metadata and controls
38 lines (31 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
void moveZerosToEnd(int arr[], int n) {
int j = 0; // Points to the next position for a non-zero element
// Traverse the array
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
// Swap non-zero element with the element at index j
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements of the array: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
moveZerosToEnd(arr, n);
printf("Array after moving zeros to the end: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}